package com.aliya.glide

import android.graphics.drawable.Animatable
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.aliya.glide.databinding.ActivityCustomTargetBinding
import com.aliya.glide.ktx.viewBindings
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.target.CustomViewTarget
import com.bumptech.glide.request.transition.Transition
import com.bumptech.glide.request.transition.Transition.ViewAdapter

/**
 * 演示 Target 是使用
 */
class CustomTargetActivity : AppCompatActivity() {
    companion object {
        const val TAG = "TransformActivity"
        const val URL = "https://t7.baidu.com/it/u=1595072465,3644073269&fm=193&f=GIF"
        const val GIF_URL = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fgif.55.la%2Fuploads%2F20210729%2F7f937f84b16ba10a429a94f582542409.gif&refer=http%3A%2F%2Fgif.55.la&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1642510579&t=58d7499e5bf4607ba7ba4eeb6c3992fb"
    }

    private val viewBindings by viewBindings(ActivityCustomTargetBinding::bind)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_custom_target)

        // 1、测试重复加载同一个 url，返回的不同的 Drawable 包裹着同一个 Bitmap
        viewBindings.ivImage0.setOnClickListener { testRepeatLoad() }
        viewBindings.ivImage2.setOnClickListener { testGifLoad() }
        // 2. 同时并发执行 load 同一个 url,只会执行一个网络请求
        viewBindings.ivImage.setOnClickListener { testMultiLoad() }

    }

    private fun testGifLoad() {
        Glide.with(this)
            .load("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202003%2F29%2F20200329043918_2FUvk.thumb.400_0.gif&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1640595089&t=eec8c62dd5f57998209441a6ec741301")
            .into(viewBindings.ivImage2)
    }

    private fun testRepeatLoad() {
        viewBindings.ivImage0.run {
            val target = Glide.with(this)
                .load(GIF_URL)
                .placeholder(R.drawable.image_placeholder)
                .transition(DrawableTransitionOptions.withCrossFade(3000))
                .into(object : CustomViewTarget<ImageView, Drawable>(this), ViewAdapter {
                    private var animatable: Animatable? = null
                    override fun onLoadFailed(errorDrawable: Drawable?) {
                        view.setImageDrawable(errorDrawable)
                    }

                    override fun onResourceReady(
                        resource: Drawable,
                        transition: Transition<in Drawable>?
                    ) {
                        // 保证过度动画生效
                        if (transition == null || !transition.transition(resource, this)) {
                            setResource(resource)
                            maybeUpdateAnimatable(resource)
                        } else {
                            maybeUpdateAnimatable(resource)
                        }
                    }

                    override fun onResourceCleared(placeholder: Drawable?) {
                        animatable?.stop()
                        setResource(placeholder)
                    }

                    // 保证 Gif 动画
                    private fun maybeUpdateAnimatable(resource: Drawable?) {
                        if (resource is Animatable) {
                            animatable = resource
                            animatable!!.start()
                        } else {
                            animatable = null
                        }
                    }

                    private fun setResource(resource: Drawable?) {
                        view.setImageDrawable(resource)
                    }

                    override fun onStart() {
                        animatable?.start()
                    }

                    override fun onStop() {
                        animatable?.stop()
                    }

                    override fun getCurrentDrawable(): Drawable? {
                        return view.drawable
                    }

                    override fun setDrawable(drawable: Drawable?) {
                        setResource(drawable)
                    }

                })
        }
    }

    private fun testMultiLoad() {
        Glide.with(this)
            .load("https://t7.baidu.com/it/u=1595072465,3644073269&fm=193&f=GIF").preload()

        Glide.with(this)
            .load("https://t7.baidu.com/it/u=1595072465,3644073269&fm=193&f=GIF").preload()

        Glide.with(this)
            .load("https://t7.baidu.com/it/u=1595072465,3644073269&fm=193&f=GIF").preload()
    }
}